package ModelLayer;
import java.util.ArrayList;


/**
 * Write a description of class Customer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Customer extends Person
{   
    private String group;
    private ArrayList<Lease> leases;
    private ArrayList<Order> orders;
    /**
     * Constructor for objects of class Customer
     * It assigns provided data to attributes of Customer class,
     * both those generic of this class as well as those inherited from Person class
     */
    public Customer(String i_id, String i_name, String i_phone_nr, String i_address, String i_postal_code, String i_city, String i_group, ArrayList<String> lease_ids, ArrayList<String> order_ids)
    {
        super(i_id, i_name, i_phone_nr, i_address, i_postal_code, i_city);
        group = i_group;
        leases = new ArrayList<>();
        orders = new ArrayList<>();
        fill_leases(lease_ids);
        fill_orders(order_ids);
        
    }

    /**********************************************************************************GETTERS**********************************************************************************************/
    
    /**
     * Returns Customer's group.
     */
    public String get_group()
    {
        return group;
    }
    
    public ArrayList<String> get_lease_ids()
    {
        ArrayList<String> temp = new ArrayList<>();
        for (Lease lease : leases)
        {
                temp.add(lease.get_id());                
        }
    	return temp;
    }
    
    
    
    /**********************************************************************************SETTERS**********************************************************************************************/
    
    /**
     * Changes Customer's group.
     */
    public void set_group(String input)
    {
        group = input;
    }
    
    /**********************************************************************************OTHER METHODS**********************************************************************************************/
    
    public String give_string_data()
    {
        String temp1="";
        boolean first = true;
        for (Lease lease:leases)
        {
            if(first)
            {
                temp1 += lease.get_id();
                first = false;
            }
            else
            {
                temp1 += " " + lease.get_id();
            }
        }
        first = true;
        String temp2 = "";
        for (Order order : orders)
        {
            if(first)
            {
                temp2 += order.get_id();
                first = false;
            }
            else
            {
                temp2 += " " + order.get_id();
            }
        }
        
        return super.give_string_data() + String.format("\t%s\t%s\t%s", temp1, temp2, group);
    }
    
    public String printable()
    {
        return super.printable() + String.format("Group:\t\t\t\t%s", group);
    }
    
    private void fill_orders(ArrayList<String> ids)
    {
        if(!(ids.contains("")))
        {
            for(String id : ids)
            {
                add_order(id);
            }
        }
    }
    
    private void fill_leases(ArrayList<String> ids)
    {
        if(!(ids.contains("")))
        {
            for(String id : ids)
            {
                add_lease(id);
            }
        }
    }
    
    public void add_order(String id)
    {
        orders.add(OrderCollection.find_by_id(id));
    }
    
    public void add_lease(String id)
    {
        leases.add(LeaseCollection.find_by_id(id));
    }
    
    public Object[] give_array()
    {
    	Object[] attributes = { get_id(), get_name(), get_phone_nr(), get_address(), get_postal_code(), get_city(), get_group() };
    	return attributes;
    }
    
    public ArrayList<Object[]> give_paid_order_array()
    {
    	ArrayList<Object[]> data = new ArrayList<>();
    	for (Order order: orders)
    	{
    		if(order.get_is_complete())
    		{
    			Object[] instance = new Object[4];
    			instance[0] = Integer.parseInt(order.get_id());
	    		instance[1] = get_name();
	    		instance[2] = order.get_with_delivery();
	    		instance[3] = order.get_total_price();
	    		data.add(instance);
    		}
    	}
    	return data;
    }
    
    public Object[] find_order(String id, boolean paid)
    {
    	
    	for (Order order: orders)
    	{
    		if(order.get_id().equals(id) && (order.get_is_complete() == paid))
    		{
    			Object[] data = {Integer.parseInt(order.get_id()), get_name(), order.get_with_delivery(), order.get_total_price()};
    			return data;
    		}
    	}
    	return null;
    }
    
    public Object[] find_any_order(String id)
 {
    	
    	for (Order order: orders)
    	{
    		if(order.get_id().equals(id))
    		{
    			Object[] data = {Integer.parseInt(order.get_id()), get_name(), order.get_with_delivery(), order.get_total_price()};
    			return data;
    		}
    	}
    	return null;
    }
    
    
    
    public ArrayList<Object[]> give_unpaid_order_array()
    {
    	ArrayList<Object[]> data = new ArrayList<>();
    	for (Order order: orders)
    	{
    		if(!(order.get_is_complete()))
    		{
    			Object[] instance = new Object[4];
	    		instance[0] = Integer.parseInt(order.get_id());
	    		instance[1] = get_name();
	    		instance[2] = order.get_with_delivery();
	    		instance[3] = order.get_total_price();
	    		data.add(instance);
    		}
    	}
    	return data;
    }

}
